﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdSetViewWallStyle.h"
#include "CommandIds.h"
#include "../UIPlatform/CommandRegister.h"
#include "IAction.h"
#include "IDocument.h"
#include "UiDocumentViewUtils.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IUserTransaction.h"
#include "ISelection.h"
#include "IUiView.h"
#include "IGraphicsStyle.h"
#include "IHighlights.h"
#include "IModelView.h"
#include "CategoryDefine.h"
#include "IGraphicsStyleDataOverrideItems.h"
#include "IGraphicsStyleData.h"
#include "ISectionBox.h"
#include "IElementStatus.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdSetViewWallStyle)

CmdSetViewWallStyle::CmdSetViewWallStyle() : CommandBase(ID_CMD_SET_VIEW_WALL_STYLE)
{

}

CmdSetViewWallStyle::~CmdSetViewWallStyle()
{

}

bool CmdSetViewWallStyle::IsEnabled() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

bool CmdSetViewWallStyle::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

gcmp::OwnerPtr<gcmp::IAction> CmdSetViewWallStyle::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    gcmp::IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"Active Document为空", L"GDMP", L"2023-09-30");

    OwnerPtr<IUserTransaction> opUt = IUserTransaction::Create(pDoc, GBMP_TR(L"修改墙体类别样式"), true);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opUt, L"ut为空", L"GDMP", L"2023-09-30");
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"ModelView不能为空", L"GDMP", L"2023-09-30");

    // 设置三维剖切盒
    ElementId sectionBoxId = pModelView->GetSectionBoxId();
    double dSize = 5000;
    TiltBox3d tiltBox(Coordinate3d(), Box3d::Create(
        Vector3d(-dSize, -dSize, -dSize),
        Vector3d(dSize, dSize, dSize)));

    ISectionBox *pSectionBox = quick_cast<ISectionBox>(pDoc->GetElement(sectionBoxId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSectionBox, L"pSectionBox指针为空", L"GDMP", L"2023-09-30");
    pSectionBox->GetStatus()->SetIsVisible(true);
    pSectionBox->SetTiltBox(tiltBox);

    pModelView->SetIsRenderCropEnabledForSectionBox(false);
    
    // 样式重载
    std::pair<OwnerPtr<IGraphicsStyleDataOverrideItems>, OwnerPtr<IGraphicsStyleData>> opStyleOverriddenItems =
        pModelView->GetGraphicsStyleOverrideByCategoryUid(Sample::WALL_CATEGORY);
    OwnerPtr<IGraphicsStyleDataOverrideItems>& curCatViewStyleOverriddenItems = opStyleOverriddenItems.first;
    OwnerPtr<IGraphicsStyleData>& curCatViewStyleData = opStyleOverriddenItems.second;

    curCatViewStyleOverriddenItems->OverrideProjectionLineColor(true);
    curCatViewStyleData->SetProjectionLineColor(Color::Black);

    curCatViewStyleOverriddenItems->OverrideProjectionLineWidth(true);
    curCatViewStyleData->SetProjectionLineWidth(10.);

    curCatViewStyleOverriddenItems->OverrideSectionLineColor(true);
    curCatViewStyleData->SetSectionLineColor(Color::Red);

    curCatViewStyleOverriddenItems->OverrideSectionLineWidth(true);
    curCatViewStyleData->SetSectionLineWidth(10.);

    curCatViewStyleOverriddenItems->OverrideSectionFaceColor(true);
    curCatViewStyleData->SetSectionFaceColor(Color::Green);

    pModelView->OverrideGraphicsStyleByCategoryUid(Sample::WALL_CATEGORY,
        *opStyleOverriddenItems.first, *opStyleOverriddenItems.second);
    opUt->Commit();
    return nullptr;
}